{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "#coding:utf-8\n",
    "import sys\n",
    "import numpy as np\n",
    "\n",
    "sys.path.append(\"..\")\n",
    "import argparse\n",
    "from train_models.mtcnn_model import P_Net, R_Net, O_Net\n",
    "from prepare_data.loader import TestLoader\n",
    "from Detection.detector import Detector\n",
    "from Detection.fcn_detector import FcnDetector\n",
    "from Detection.MtcnnDetector import MtcnnDetector\n",
    "import cv2\n",
    "import os\n",
    "\n",
    "data_dir = '../../DATA/WIDER_val/images'\n",
    "anno_file = 'wider_face_val.txt'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_gt_bbox(raw_list):\n",
    "    \n",
    "    list_len = len(raw_list)\n",
    "    bbox_num = (list_len-1)//4\n",
    "    idx = 1\n",
    "    bboxes = np.zeros((bbox_num,4),dtype=int)\n",
    "    for i in range(4):\n",
    "        for j in range(bbox_num):\n",
    "            bboxes[j][i] = int(raw_list[idx])\n",
    "            idx += 1\n",
    "    return bboxes\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_image_info(anno_file):\n",
    "    f = open(anno_file,'r')\n",
    "    image_info = []\n",
    "    for line in f:\n",
    "        ct_list = line.strip().split(' ')\n",
    "        path = ct_list[0]\n",
    "        \n",
    "        path_list = path.split('\\\\')\n",
    "        event = path_list[0]\n",
    "        name = path_list [1]\n",
    "        #print(event, name )\n",
    "        bboxes = read_gt_bbox(ct_list)\n",
    "        image_info.append([event,name,bboxes])\n",
    "    print('total number of images in validation set: ', len(image_info))\n",
    "    return image_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_mode = \"ONet\"\n",
    "thresh = [0.6,0.5,0.4]\n",
    "min_face_size = 24\n",
    "stride = 2\n",
    "slide_window = False\n",
    "shuffle = False\n",
    "vis = False\n",
    "detectors = [None, None, None]\n",
    "prefix = ['../data/MTCNN_model/PNet_landmark/PNet', '../data/MTCNN_model/RNet_landmark/RNet', '../data/MTCNN_model/ONet_landmark/ONet']\n",
    "epoch = [18, 14, 16]\n",
    "batch_size = [2048, 256, 16]\n",
    "model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, ?, ?, 3)\n(1, ?, ?, 10)\n(1, ?, ?, 10)\n(1, ?, ?, 16)\n(1, ?, ?, 32)\n(1, ?, ?, 2)\n(1, ?, ?, 4)\n(1, ?, ?, 10)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../data/MTCNN_model/PNet_landmark/PNet-18\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "the params dictionary is not valid",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-88372b16479f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mPNet\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDetector\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mP_Net\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel_path\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m     \u001b[0mPNet\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFcnDetector\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mP_Net\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel_path\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0mdetectors\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPNet\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mE:\\Models\\New folder\\MTCNN-Tensorflow\\Detection\\fcn_detector.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, net_factory, model_path)\u001b[0m\n\u001b[1;32m     33\u001b[0m             \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel_path\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     34\u001b[0m             \u001b[0mreadstate\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mckpt\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mckpt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel_checkpoint_path\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m             \u001b[0;32massert\u001b[0m  \u001b[0mreadstate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the params dictionary is not valid\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     36\u001b[0m             \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"restore models' param\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     37\u001b[0m             \u001b[0msaver\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrestore\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msess\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel_path\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAssertionError\u001b[0m: the params dictionary is not valid"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "\n",
    "if slide_window:\n",
    "    PNet = Detector(P_Net, 12, batch_size[0], model_path[0])\n",
    "else:\n",
    "    PNet = FcnDetector(P_Net, model_path[0])\n",
    "detectors[0] = PNet\n",
    "    \n",
    "# load rnet model\n",
    "if test_mode in [\"RNet\", \"ONet\"]:\n",
    "    RNet = Detector(R_Net, 24, batch_size[1], model_path[1])\n",
    "    detectors[1] = RNet\n",
    "    \n",
    "# load onet model\n",
    "if test_mode == \"ONet\":\n",
    "    ONet = Detector(O_Net, 48, batch_size[2], model_path[2])\n",
    "    detectors[2] = ONet\n",
    "    \n",
    "mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,\n",
    "                                   stride=stride, threshold=thresh, slide_window=slide_window)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'wider_face_val.txt'",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-dd626f803858>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mimage_info\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_image_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0manno_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-4-a10cefb9937b>\u001b[0m in \u001b[0;36mget_image_info\u001b[0;34m(anno_file)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_image_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0manno_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m     \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0manno_file\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m     \u001b[0mimage_info\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m         \u001b[0mct_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mline\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'wider_face_val.txt'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "image_info = get_image_info(anno_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "str1='aaa'\n",
    "str2 = 'bbb'\n",
    "str3 = 'aaa'\n",
    "print(str1 != str2)\n",
    "print (str1 == str3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['asdfasdf', '']"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ='asdfasdf.jpg'\n",
    "a.split('.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'MtcnnDetector' object has no attribute 'detect_single_image'",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-98-438830302ed7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0mdets_file_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msave_path\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mf_name\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'.txt'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m     \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage_file_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m     \u001b[0mall_boxes\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmtcnn_detector\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetect_single_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     14\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage_file_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mbbox\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mall_boxes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'MtcnnDetector' object has no attribute 'detect_single_image'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "current_event = ''\n",
    "save_path = ''\n",
    "for item in image_info:\n",
    "    image_file_name = os.path.join(data_dir,item[0],item[1])\n",
    "    if current_event != item[0]:\n",
    "        current_event = item[0]\n",
    "        save_path = os.path.join('../../DATA',item[0])\n",
    "        if not os.path.exists(save_path):\n",
    "            os.mkdir(save_path)\n",
    "    f_name= item[1].split('.jpg')[0]\n",
    "    dets_file_name = os.path.join(save_path,f_name + '.txt')\n",
    "    img = cv2.imread(image_file_name)\n",
    "    all_boxes,_ = mtcnn_detector.detect_single_image(img)\n",
    "      \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
